/**
 * Created by jinyingchao on 17/5/19.
 */


$(document).ready(function() {

    // 功能1 侧栏导航
    $('.dropdown').on('show.bs.dropdown', function(e){
        $(this).find('.dropdown-menu').first().stop(true, true).slideDown(300);
    });
    $('.dropdown').on('hide.bs.dropdown', function(e){
        $(this).find('.dropdown-menu').first().stop(true, true).slideUp(300);
    });
    $("#menu-toggle").click(function(e) {
        e.preventDefault();
        var elem = document.getElementById("sidebar-wrapper");
        left = window.getComputedStyle(elem,null).getPropertyValue("left");
        if(left == "200px"){
            document.getElementsByClassName("sidebar-toggle")[0].style.left="-200px";
        }
        else if(left == "-200px"){
            document.getElementsByClassName("sidebar-toggle")[0].style.left="200px";
        }

        $("body").click(function(){  /* 函数1 */
            document.getElementsByClassName("sidebar-toggle")[0].style.left="-200px";
        });

        $(document).on("click", function(){
            document.getElementsByClassName("sidebar-toggle")[0].style.left="-200px";
        });

        e.stopPropagation();
    });

    // $(window).resize(function() {
    //     var path = $(this);
    //     var contW = path.width();
    //     if(contW >= 751){
    //         document.getElementsByClassName("sidebar-toggle")[0].style.left="200px";
    //     }else{
    //         document.getElementsByClassName("sidebar-toggle")[0].style.left="-200px";
    //     }
    // });

//    $(window).on('hashchange', function(){
//        var url = window.location.href;
//        var target;
//        var re = /(#.*)/;
//        var result = re.exec(url);
//        if (result != null) {
//            target = result[0].replace('#', '');
//            $.ajax({
//                type: 'GET',
//                url: '/' + target,
//                error: function () {
//                },
//                success: function (res) {
//                    // document.getElementById('dynamic').innerHTML = res;
//                    $('#dynamic').html(res);
//                    $('#editable-select').editableSelect();
//                }
//            });
//        }
//
//    });

    // 侧向导航栏功能
    var trigger = $('.hamburger'),
        overlay = $('.overlay'),
        isClosed = false;

    trigger.click(function () {
        hamburger_cross();
    });

    function hamburger_cross() {

        if (isClosed == true) {
            overlay.hide();
            trigger.removeClass('is-closed');
            trigger.addClass('is-open');
            isClosed = false;
        } else {
            overlay.show();
            trigger.removeClass('is-open');
            trigger.addClass('is-closed');
            isClosed = true;
        }
    }

    $('[data-toggle="offcanvas"]').click(function () {
        $('#wrapper').toggleClass('toggled');
    });

    // 功能3 尾部动态加载
    // var footer = $(".footer");
    // footer.html("Copyright © 2017 竹间智能科技（上海）有限公司");

    // 功能4  左侧导航高亮
    function hightlight() {
        var url = location.pathname;
        var str = url.slice(url.length-7,url.length-5);
        var a = str.slice(0,1);
        if (isNaN(a)) {
            str = str.slice(1);
        }
        var sidebarlist = $('.sidebar-nav li');
        for (var i = 0; i<sidebarlist.length; i++) {
            sidebarlist.eq(str-1).addClass('hightlight').siblings().removeClass('hightlight');
        }
    }

    var parentElement = $('.col-lg-10');

    // 文件上传 + 获取
    parentElement.on('click', '#upload', function () {
        var project_name = $('#editable-select').val();
        var filename = $('#filename').val();
        var update_mode = $('#updatemode').val();
        if (project_name == '') {
            $('#myModal').modal('show');
            return;
        } else if (filename == '') {
            $('#myModal1').modal('show');
            return;
        } else if (update_mode == '') {
            $('#myModal2').modal('show');
            return;
        }

        var l = Ladda.create(this);
        l.start();
        $('#import_result').html('正在上传...');
        var formData = new FormData($("#uploadForm")[0]);
        $.ajax({
            type: "POST",
            url: '/upload',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            error: function () {
            },
            success: function (res) {
                console.log('执行stop function');
                l.stop();
                $('#import_result').html(res.desc);
            }

        });
    });

    parentElement.on('click', '#customupload', function () {
        var project_name = $('#editable-select').val();
        var trainfile = $('#trainfile').val();
        var validatefile = $('#validatefile').val();
        if (project_name == '') {
            $('#myModal').modal('show');
            return;
        } else if (trainfile == '' || validatefile == '') {
            $('#myModal1').modal('show');
            return;
        }
        var l = Ladda.create(this);
        l.start();
        $('#import_result').html('正在上传...');
        var formData = new FormData($('#customform')[0]);
        $.ajax({
            type: 'POST',
            url: '/custom',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            error: function () {
            },
            success: function (res) {
                l.stop();
                $('#import_result').html(res.desc);
            }
        });
    });

});
